package com.vercon.moensmart.dialog

import android.net.Uri
import android.os.Handler
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.vercon.common.mode.common.BaseMode
import com.vercon.common.mode.weather.AirAuality
import com.vercon.common.presenter.common.MirrorResultView
import com.vercon.common.utils.LogUtils
import com.vercon.common.utils.WeatherUtils
import com.vercon.moensmart.R
import com.vercon.moensmart.databinding.DialogToiletControllerLayoutBinding
import com.vercon.moensmart.dialog.base.BaseDialogFragment
import com.vercon.moensmart.mode.request.DeviceCommandRequest
import com.vercon.moensmart.mode.response.BaseResponse
import com.vercon.moensmart.presenter.MoenPresenter
import com.vercon.moensmart.utils.FrescoUtil
import com.vercon.moensmart.utils.ToastUtils
import org.json.JSONObject
import java.util.Objects

class ToiletControllerDialog : BaseDialogFragment<DialogToiletControllerLayoutBinding>() {


    val moenPresenter = MoenPresenter()
    var deviceId : String? = null
    var productId : String? = null
    val mGson = Gson()
    companion object{
        val DEVICE_ID = "DEVICE_ID"
        val PRODUCT_ID = "PRODUCT_ID"
        val PRODUCT_PIC = "PRODUCT_PIC"
        val PRODUCT_NAME = "PRODUCT_NAME"
    }
    override fun onStart() {
        super.onStart()
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        dialog!!.window!!.setLayout(resources.getDimension(com.vercon.common.R.dimen.dp_950).toInt(), resources.getDimension(com.vercon.common.R.dimen.dp_885).toInt())
    }

    override fun onCreateViewBinding(inflater: LayoutInflater, parent: ViewGroup?): DialogToiletControllerLayoutBinding {
        return DialogToiletControllerLayoutBinding.inflate(inflater,parent,false)
    }

    override fun initView(view: View?) {
        deviceId = arguments?.getString(DEVICE_ID,"")
        productId = arguments?.getString(PRODUCT_ID,"")
        if(arguments?.getString(PRODUCT_PIC,"").isNullOrEmpty()){
            FrescoUtil.loadImage(binding?.pictureImageview, Uri.parse("res://" + context?.packageName + "/" + R.mipmap.ic_toilet))
        }else{
            FrescoUtil.loadImage(binding?.pictureImageview, Uri.parse(arguments?.getString(PRODUCT_PIC,"")))
        }

        binding?.tvSmallTitle?.text = "产品型号 " + arguments?.getString(PRODUCT_NAME,"")
    }

    override fun initListener(view: View?) {

        //喷嘴保养
        binding?.vSwitch1?.setOnClickListener {
            binding?.vSwitch1?.isSelected = !binding?.vSwitch1?.isSelected!!
            val command = HashMap<String,Any>()
            if(binding?.vSwitch1?.isSelected!!){
                command["nozzle_maintaining"] = 1
                sendCommand(command)
            }else{
                command["nozzle_maintaining"] = 0
                sendCommand(command)
            }
        }

        //夜灯
        binding?.vSwitch2?.setOnClickListener {
            val command = HashMap<String,Any>()
            binding?.vSwitch2?.isSelected = !binding?.vSwitch2?.isSelected!!
            if(!binding?.vSwitch2?.isSelected!!){
                binding?.igSwitch2InductiveSwitch?.isSelected = false
                binding?.tvInductiveSwitchText?.setTextColor(resources.getColor(com.vercon.common.R.color.color_4D9DB4))

                command["led_switch"] = 1
                sendCommand(command)
            }else{
                binding?.tvInductiveSwitchText?.setTextColor(resources.getColor(com.vercon.common.R.color.white))
                command["led_switch"] = 0
                sendCommand(command)
            }
        }
        binding?.vSwitch3?.setOnClickListener {
            binding?.vSwitch3?.isSelected = !binding?.vSwitch3?.isSelected!!
            if(binding?.vSwitch3?.isSelected!!){
                binding?.tvCloseToiletLid?.setTextColor(resources.getColor(com.vercon.common.R.color.white))
                binding?.tvCloseToiletSeat?.setTextColor(resources.getColor(com.vercon.common.R.color.white))
                binding?.igLidSeatLine?.setImageResource(R.mipmap.ic_device_switch_line_open)
            }else{
                binding?.tvCloseToiletLid?.setTextColor(resources.getColor(com.vercon.common.R.color.color_4D9DB4))
                binding?.tvCloseToiletSeat?.setTextColor(resources.getColor(com.vercon.common.R.color.color_4D9DB4))
                binding?.igLidSeatLine?.setImageResource(R.mipmap.ic_device_switch_line_close)
            }
        }
        binding?.vSwitch4?.setOnClickListener {
            binding?.vSwitch4?.isSelected = !binding?.vSwitch4?.isSelected!!
            if(binding?.vSwitch4?.isSelected!!){
                binding?.tvBig?.setTextColor(resources.getColor(com.vercon.common.R.color.white))
                binding?.tvSmall?.setTextColor(resources.getColor(com.vercon.common.R.color.white))
                binding?.igBigSmallLine?.setImageResource(R.mipmap.ic_device_switch_line_open)
            }else{
                binding?.tvBig?.setTextColor(resources.getColor(com.vercon.common.R.color.color_4D9DB4))
                binding?.tvSmall?.setTextColor(resources.getColor(com.vercon.common.R.color.color_4D9DB4))
                binding?.igBigSmallLine?.setImageResource(R.mipmap.ic_device_switch_line_close)
            }
        }
        binding?.igSwitch2InductiveSwitch?.setOnClickListener {
            binding?.igSwitch2InductiveSwitch?.isSelected = !binding?.igSwitch2InductiveSwitch?.isSelected!!
        }

        binding?.tvCloseToiletLid?.setOnClickListener { ToastUtils.showToast("已执行") }
        binding?.tvCloseToiletSeat?.setOnClickListener { ToastUtils.showToast("已执行") }
        binding?.tvBig?.setOnClickListener { ToastUtils.showToast("已执行") }
        binding?.tvSmall?.setOnClickListener { ToastUtils.showToast("已执行") }

        binding?.igSpringSwitch?.setOnClickListener {
            theFourSeasonsSwitch(binding?.igSpringSwitch?.isSelected!!,1)
        }
        binding?.igSummerSwitch?.setOnClickListener {
            theFourSeasonsSwitch(binding?.igSummerSwitch?.isSelected!!,2)
        }
        binding?.igAutumnSwitch?.setOnClickListener {
            theFourSeasonsSwitch(binding?.igAutumnSwitch?.isSelected!!,3)
        }
        binding?.igWinterSwitch?.setOnClickListener {
            theFourSeasonsSwitch( binding?.igWinterSwitch?.isSelected!!,4)
        }
        //休眠模式
        binding?.tvSleepMode?.setOnClickListener {
            val command = HashMap<String,Any>()
            binding?.tvSleepMode?.isSelected = !binding?.tvSleepMode?.isSelected!!
            binding?.tvPowerSavingMode?.isSelected = false

            if(binding?.tvSleepMode?.isSelected!!){
                command["sleep_mode"] = 1
                sendCommand(command)
            }else{
                command["sleep_mode"] = 0
                sendCommand(command)
            }
        }
        binding?.tvPowerSavingMode?.setOnClickListener {
            binding?.tvPowerSavingMode?.isSelected = !binding?.tvPowerSavingMode?.isSelected!!
            binding?.tvSleepMode?.isSelected = false
        }

        binding?.igClose?.setOnClickListener { dismiss() }
    }

    fun theFourSeasonsSwitch(isSelect : Boolean,type : Int){
        val command = HashMap<String,Any>()
        binding?.igSpringSwitch?.isSelected = !isSelect && type == 1
        binding?.igSummerSwitch?.isSelected = !isSelect && type == 2
        binding?.igAutumnSwitch?.isSelected = !isSelect && type == 3
        binding?.igWinterSwitch?.isSelected = !isSelect && type == 4
        when(type){
            1->{

            }
            2->{

            }
            3->{

            }
            4->{

            }
        }
    }

    private fun sendCommand(command : HashMap<String,Any>){
        if(deviceId.isNullOrEmpty() || productId.isNullOrEmpty()){
            return
        }
        val request = DeviceCommandRequest(deviceId,productId,DeviceCommandRequest.Command(command))
//        moenPresenter.sendDeviceCommand(request, object : MirrorResultView<BaseResponse<Any>> {
//            override fun onSuccess(result: BaseResponse<Any>?) {
//                getDeviceInfo()
//            }
//            override fun onError(code: Int, message: String?) {
//
//            }
//        })
    }

    private fun getDeviceInfo(){
//        moenPresenter.getDeviceInfo(deviceId,productId,object  : MirrorResultView<BaseResponse<Any>>{
//            override fun onSuccess(result: BaseResponse<Any>?) {
//                LogUtils.e("ToiletController","  getDeviceInfo ： " + Gson().toJson(result))
//
//            }
//            override fun onError(code: Int, message: String?) {
//
//            }
//        })
    }

    override val isFullScreen: Boolean
        get() = false

}